﻿<html DIR="LTR" xmlns:tool="http://www.microsoft.com/tooltip" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ddue="http://ddue.schemas.microsoft.com/authoring/2003/5" xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
  <head>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=utf-8" />
    <META NAME="save" CONTENT="history" />
    <title>Basic Data Types Sample</title>
    
    <link rel="stylesheet" type="text/css" href="../local/Classic.css">
      
    </link>
    
    <script src="../local/script.js">
      
    </script><script src="../local/script_main.js">&amp;nbsp;</script>
  </head>
  <body>
    <!--Topic built:09/24/2007 09:01:38-->

    
    
    
    
    
    
    
    
    
    <div id="header">
      <table width="100%" id="topTable"><tr>
          <td align="left">
            <span id="headerBold">Basic Data Types Sample</span>
          </td>
          <td align="right">
            
          </td>
        </tr></table>
      
      
      
    </div>
    <div id="mainSection">
      
        
        
		<font color="DarkGray"><!-- [This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]<br/>[Documentation built on $$TIMESTAMP$$]  --> </font> <p /> <p />
	
        <div id="introductionSection" class="section">
    <p>This Microsoft SQL Server 2005 JDBC Driver sample application demonstrates how to use result set getter methods to retrieve basic SQL Server data type values, and how to use result set update methods to update those values. </p>
    <p>The code file for this sample is named basicDT.java, and it can be found in the following location:</p>
    <p>&lt;<i>installation directory</i>&gt;\sqljdbc_&lt;<i>version</i>&gt;\&lt;<i>language</i>&gt;\help\samples\datatypes</p>
  </div><h1 class="heading">Requirements</h1><div id="requirementsSection" class="section">
    <content xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <p xmlns="">To run this sample application, you must set the classpath to include the sqljdbc.jar file. If the classpath is missing an entry for sqljdbc.jar, the sample application will throw the common "Class not found" exception. You will also need access to the SQL Server 2005 AdventureWorks sample database. For more information about how to set the classpath, see <a href="6faaf05b-8b70-4ed2-9b44-eee5897f1cd0.htm">Using the JDBC Driver</a>.</p>
      <p xmlns="">You must also create the following table and sample data in the SQL Server 2005 AdventureWorks sample database:</p>
      <div class="sampleCode" xmlns=""><span codeLanguage="other"><pre>CREATE TABLE DataTypesTable 
   (Col1 int IDENTITY, 
    Col2 char,
    Col3 varchar(50), 
    Col4 bit,
    Col5 decimal(18, 2),
    Col6 money,
    Col7 datetime);

INSERT INTO DataTypesTable 
VALUES ('A', 'Some text.', 0, 15.25, 10.00, '01/01/2006 23:59:59.991');</pre></span></div>
    </content>
  </div><h1 class="heading">Example</h1><div id="codeExampleSection" class="section">
    <description xmlns="http://ddue.schemas.microsoft.com/authoring/2003/5">
      <content>
        <p xmlns="">In the following example, the sample code makes a connection to the SQL Server 2005 AdventureWorks database, and then retrieves a single row of data from the DataTypesTable test table. The custom <b>displayRow</b> method is then called to display all the data contained in the result set using various <b>get&lt;Type&gt;</b> methods of the <a href="eaffcff1-286c-459f-83da-3150778480c9.htm">SQLServerResultSet</a> class.</p>
        <p xmlns="">Next, the sample uses various <b>update&lt;Type&gt;</b> methods of the <b>SQLServerResultSet</b> class to update the data contained in the result set, and then calls the <a href="cfced0ca-a281-40dc-8d2f-370d5f0bf12b.htm">updateRow</a> method to persist that data back to the database.</p>
        <p xmlns="">Finally, the sample refreshes the data contained in the result set and then calls the custom <b>displayRow</b> method again to display the data contained in the result set.</p>
      </content>
    </description>
    <div class="sampleCode"><span codeLanguage="other"><pre>import java.sql.*;

public class basicDT {

   public static void main(String[] args) {

      // Create a variable for the connection string.
      String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
            "databaseName=AdventureWorks;integratedSecurity=true;";

      // Declare the JDBC objects.
      Connection con = null;
      Statement stmt = null;
      ResultSet rs = null;

      try {
         // Establish the connection.
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
         con = DriverManager.getConnection(connectionUrl);

         // Create and execute an SQL statement that returns some data
         // and display it.
         String SQL = "SELECT * FROM DataTypesTable;";
         stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE);
         rs = stmt.executeQuery(SQL);
         rs.next();
         displayRow("ORIGINAL DATA", rs);

         // Update the data in the result set.
         rs.updateString(2, "B");
         rs.updateString(3, "Some updated text.");
         rs.updateBoolean(4, true);
         rs.updateDouble(5, 77.89);
         rs.updateDouble(6, 1000.01);
         rs.updateTimestamp(7, new Timestamp(System.currentTimeMillis()));
         rs.updateRow();

         // Get the updated data from the database and display it.
         rs = stmt.executeQuery(SQL);
         rs.next();
         displayRow("UPDATED DATA", rs);
      }

      // Handle any errors that may have occurred.
      catch (Exception e) {
         e.printStackTrace();
      }

      finally {
         if (rs != null) try { rs.close(); } catch(Exception e) {}
         if (stmt != null) try { stmt.close(); } catch(Exception e) {}
         if (con != null) try { con.close(); } catch(Exception e) {}
      }
   }

   public static void displayRow(String title, ResultSet rs) {
      try {
         System.out.println(title);
         System.out.println(rs.getInt(1) + " " +  // SQL integer type.
               rs.getString(2) + " " +            // SQL char type.
               rs.getString(3) + " " +            // SQL varchar type.
               rs.getBoolean(4) + " " +           // SQL bit type.
               rs.getDouble(5) + " " +            // SQL decimal type.
               rs.getDouble(6) + " " +            // SQL money type.
               rs.getTimestamp(7));               // SQL datetime type.
         System.out.println();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}</pre></span></div>
  </div><span id="seeAlsoSpan"><h1 class="heading">See Also</h1></span><div id="seeAlsoSection" class="section" name="collapseableSection"><a href="b39f44d0-3710-4bc6-880c-35bd8c10a734.htm">Working with Data Types (JDBC)</a><br /><br /></div><!--[if gte IE 5]>
			<tool:tip element="seeAlsoToolTip" avoidmouse="false"/><tool:tip element="languageFilterToolTip" avoidmouse="false"/><tool:tip element="roleInfoSpan" avoidmouse="false"/>
		<![endif]-->
      <div id="footer" class="section">
        
		<hr />
		
		<span id="fb" class="feedbackcss">
			
			
		</span>
		
		<a href="9bad553b-9e70-4696-8499-2e35f772a1e0.htm">
			
			© 2007 Microsoft Corporation. All rights reserved.
		</a>
 	
	
      </div>
    </div>
  </body>
</html>